package com.cspg.design.request;

import android.app.Activity;

import com.cspg.design.CSPGApplication;
import com.cspg.design.bean.modelLibrary.TowerLibrary;
import com.cspg.design.bean.modelLibrary.TransformerLibrary;
import com.cspg.design.bean.modelLibrary.WireLibrary;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.dto.PoleLibraryDto;
import com.cspg.design.dto.TransformerLibraryDto;
import com.cspg.design.dto.WireLibraryDto;
import com.cspg.design.http.IService;
import com.cspg.design.http.RetrofitUtil;
import com.cspg.design.model.PoleLibraryModel;
import com.cspg.design.model.TransformerLibraryModel;
import com.cspg.design.model.WireLibraryModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DeviceLibrary {

    private final DaoSession daoSession;
    private Activity activity;
    public DeviceLibrary(Activity activity)
    {
        this.activity = activity;
        daoSession = ((CSPGApplication)activity.getApplication() ).getDaoSession();
    }

    public DeviceLibrary(DaoSession daoSession){
        this.daoSession = daoSession;
    }

    public void requestPoleLibrary()
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        Call call = retrofitUtil.createReq(IService.class).getPoleLibrary(0,10000);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    return;
                }

                if (!response.isSuccessful()) {
                    return;
                }
                Object obj = response.body();
                PoleLibraryModel poleLibraryModel= (PoleLibraryModel) response.body();
                if (null == poleLibraryModel) {
                    return;
                }
                else
                {
                    savePoleLibraryToLocal(poleLibraryModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                System.out.println("杆塔失败");
            }
        });
    }

    public void requestTransformerLibrary()
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        Call call = retrofitUtil.createReq(IService.class).getTransformerLibrary(0,10000);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    return;
                }

                if (!response.isSuccessful()) {
                    return;
                }
                Object obj = response.body();
                TransformerLibraryModel transformerLibraryModel= (TransformerLibraryModel) response.body();
                if (null == transformerLibraryModel) {
                    return;
                }
                else
                {
                    saveTransformerLibraryToLocal(transformerLibraryModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                System.out.println("变压器失败");
            }
        });
    }

    public void requestWireLibrary()
    {
        RetrofitUtil retrofitUtil = new RetrofitUtil();
        Call call = retrofitUtil.createReq(IService.class).getWireLibrary(0,10000);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (null == response) {
                    return;
                }

                if (!response.isSuccessful()) {
                    return;
                }
                Object obj = response.body();
                WireLibraryModel wireLibraryModel= (WireLibraryModel) response.body();
                if (null == wireLibraryModel) {
                    return;
                }
                else
                {
                    saveWireLibraryToLocal(wireLibraryModel);
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                System.out.println("导线失败");
            }
        });
    }

    public void savePoleLibraryToLocal(PoleLibraryModel poleLibraryModel)
    {
        List<PoleLibraryDto> poleLibraryDtoList = poleLibraryModel.getContent();
        daoSession.getTowerLibraryDao().deleteAll();
        if (daoSession.getTowerLibraryDao().queryBuilder().list().size() == 0)
        {
            daoSession.getTowerLibraryDao().rx().insertOrReplaceInTx(poleLibraryDtoListConverterToBeans(poleLibraryDtoList)).subscribe();
        }
    }

    public List<TowerLibrary> poleLibraryDtoListConverterToBeans(List<PoleLibraryDto> poleLibraryDtoList)
    {
        List<TowerLibrary> towerLibraryList = new ArrayList<>();
        for (int i =0 ; i< poleLibraryDtoList.size();i++)
        {
            towerLibraryList.add(poleLibraryDtoConverterToBean(poleLibraryDtoList.get(i)));
        }
        return towerLibraryList;
    }
    public TowerLibrary poleLibraryDtoConverterToBean(PoleLibraryDto poleLibraryDto)
    {
        TowerLibrary towerLibrary = new TowerLibrary();
        towerLibrary.setLibId(poleLibraryDto.getId());
        towerLibrary.setPoleTypeCodeID(poleLibraryDto.getPoleTypeCodeId());
        towerLibrary.setPoleModel(poleLibraryDto.getPoleModel());
        towerLibrary.setCornerDegree(poleLibraryDto.getCornerDegree());
        towerLibrary.setDistance(poleLibraryDto.getDistance());
        towerLibrary.setMaxCornerDegree(poleLibraryDto.getMaxCornerDegree());
        towerLibrary.setMaxWireCrossSection(poleLibraryDto.getMaxWireCrossSection());
        towerLibrary.setMiniCornerDegree(poleLibraryDto.getMiniCornerDegree());
        towerLibrary.setMiniWireCrossSection(poleLibraryDto.getMiniCornerDegree());
        towerLibrary.setPoleHeight(poleLibraryDto.getPoleHeight());
        towerLibrary.setTipDiameter(poleLibraryDto.getTipDiameter());
        towerLibrary.setVoltageLevel(poleLibraryDto.getVoltageLevel());
        towerLibrary.setWireQuality(poleLibraryDto.getWireQuality());
        return  towerLibrary;
    }
    public void saveWireLibraryToLocal(WireLibraryModel wireLibraryModel)
    {
        List<WireLibraryDto> wireLibraryDtoList = wireLibraryModel.getContent();
        daoSession.getWireLibraryDao().deleteAll();
        if (daoSession.getWireLibraryDao().queryBuilder().list().size() == 0)
        {
            daoSession.getWireLibraryDao().rx().insertOrReplaceInTx(wireLibraryDtoListConverterToBeans(wireLibraryDtoList)).subscribe();
        }
    }

    public List<WireLibrary> wireLibraryDtoListConverterToBeans(List<WireLibraryDto> wireLibraryDtoList)
    {
        List<WireLibrary> wireLibraries = new ArrayList<>();
        for (int i =0 ; i< wireLibraryDtoList.size();i++)
        {
            wireLibraries.add(wireLibraryDtoConverterToBean(wireLibraryDtoList.get(i)));
        }
        return wireLibraries;
    }
    public WireLibrary wireLibraryDtoConverterToBean(WireLibraryDto wireLibraryDto)
    {
        WireLibrary wireLibrary = new WireLibrary();
        wireLibrary.setLibId(wireLibraryDto.getId());
        wireLibrary.setVoltageLevel(wireLibraryDto.getVoltageLevel());
        wireLibrary.setWireCrossSection(wireLibraryDto.getWireCrossSection());
        wireLibrary.setWireID(wireLibraryDto.getWireId());
        wireLibrary.setWireModel(wireLibraryDto.getWireModel());
        wireLibrary.setWireQuality(wireLibraryDto.getWireQuality());
        wireLibrary.setCostPerKM(wireLibraryDto.getCostPerKM());
        return  wireLibrary;
    }

    public void saveTransformerLibraryToLocal(TransformerLibraryModel transformerLibraryModel)
    {
        List<TransformerLibraryDto> transformerLibraryDtoList = transformerLibraryModel.getContent();
        daoSession.getTransformerLibraryDao().deleteAll();
        if (daoSession.getTransformerLibraryDao().queryBuilder().list().size() == 0)
        {
            daoSession.getTransformerLibraryDao().rx().insertOrReplaceInTx(transformerLibraryDtoListConverterToBeans(transformerLibraryDtoList)).subscribe();
        }
    }

    public List<TransformerLibrary> transformerLibraryDtoListConverterToBeans(List<TransformerLibraryDto> transformerLibraryDtoList)
    {
        List<TransformerLibrary> transformerLibraries = new ArrayList<>();
        for (int i =0 ; i< transformerLibraryDtoList.size();i++)
        {
            transformerLibraries.add(transformerLibraryDtoConverterToBean(transformerLibraryDtoList.get(i)));
        }
        return transformerLibraries;
    }
    public TransformerLibrary transformerLibraryDtoConverterToBean(TransformerLibraryDto transformerLibraryDto)
    {
        TransformerLibrary transformerLibrary = new TransformerLibrary();
        transformerLibrary.setLibId(transformerLibraryDto.getId());
        transformerLibrary.setTransformerModel(transformerLibraryDto.getTransformerModel());
        transformerLibrary.setTransformerTypeCodeID(transformerLibraryDto.getTransformerTypeCodeId());
        transformerLibrary.setPoleModel(transformerLibraryDto.getPoleModel());
        transformerLibrary.setPoleHeight(transformerLibraryDto.getPoleHeight());
        transformerLibrary.setCapacity(transformerLibraryDto.getCapacity());
        transformerLibrary.setTo380(transformerLibraryDto.getTo380());
        transformerLibrary.setTo220(transformerLibraryDto.getTo220());
        transformerLibrary.setCost(transformerLibraryDto.getCost());
        return  transformerLibrary;
    }
}
